package com.mycompany.project.client.chee;

import java.util.ArrayList;

public class ModuleList {
	//the list of modules -- use this if you need to use the modules in the list
	public static ArrayList<ArrayList<Module>> mList = new ArrayList<ArrayList<Module>>();
	private static int []usable = new int[10];
	
	public static ArrayList<ArrayList<Module>> getMlist() {
		return mList;
	}
	public static void initMList()
	{
		for(int i=0;i<Module.MAX_SIZE;i++)
		{
			mList.add(new ArrayList<Module>());
		}
	}
	public static int getTotalModules()
	{
		int count = 0;
		for(int i=0;i<mList.size();i++)
		{
			count += mList.get(i).size();
		}
		return count;
	}
	/**
	 * Given a code number, this method check the module list to see if that module exist.
	 * If it exist, it will return the index to that list. Else return -1
	 * @param code
	 * @return index to module, else -1
	 */
	public static int getModule(int code)
	{
		for(int i=0;i<ModuleList.mList.size();i++)
		{
			for(int j=0;j<ModuleList.mList.get(i).size();j++)
			{
				if(ModuleList.mList.get(i).get(j).getCodeNumber() == code)
				{
					return j;
				}
			}
		}
		return -1;
	}
	public static int getTotalUsableCount()
	{
		int usable = 0;
		for(int i=0;i<ModuleList.mList.size();i++)
		{
			usable +=getModuleUsable(i);
		}
		return usable;
	}
	public static int getTotalUsableAfterRepairCount()
	{
		int usableAfterRepair = 0;
		for(int i=0;i<ModuleList.mList.size();i++)
		{
			usableAfterRepair +=getModuleUsableAfterRepair(i);
		}
		return usableAfterRepair;
	}
	public static int getTotalBeyondRepairCount()
	{
		int beyondRepair = 0;
		for(int i=0;i<ModuleList.mList.size();i++)
		{
			beyondRepair +=getModuleBeyondRepair(i);
		}
		return beyondRepair;
	}
	/**
	 * 
	 * @param type the module tyupe
	 * @return the number of modules that are usable after repair
	 */
	public static int getModuleUsableAfterRepair(int type)
	{
		int usableAfterRepair = 0;
		for(int i=0;i<ModuleList.mList.get(type).size();i++)
		{
			if(ModuleList.mList.get(type).get(i).getStats().equals("usable after repair"))
			{
				usableAfterRepair++;
			}
		}
		return usableAfterRepair;
	}
	public static int getModuleBeyondRepair(int type)
	{
		int beyondRepair = 0;
		for(int i=0;i<ModuleList.mList.get(type).size();i++)
		{
			if(ModuleList.mList.get(type).get(i).getStats().equals("beyond repair"))
			{
				beyondRepair++;
			}
		}
		return beyondRepair;
	}
	/**
	 * 
	 * @param type the module type
	 * @return the number of modules that are usable
	 */
	public static int getModuleUsable(int type)
	{
		int usable = 0;
		for(int i=0;i<ModuleList.mList.get(type).size();i++)
		{
			if(ModuleList.mList.get(type).get(i).getStats().equals("usable"))
			{
				usable++;
			}
		}
		return usable;
	}
	/**
	 * Given a code number, this method check the module list to see if that module exist.
	 * If it exist, it will return the index to that list. Else return -1
	 * @param code
	 * @return index to module type, else -1
	 */
	public static int getModuleListType(int code)
	{
		for(int i=0;i<ModuleList.mList.size();i++)
		{
			for(int j=0;j<ModuleList.mList.get(i).size();j++)
			{
				if(ModuleList.mList.get(i).get(j).getCodeNumber() == code)
				{
					return i;
				}
			}
		}
		return -1;
	}
	/**
	 * 
	 * @param type the module type
	 * @return the number of modules of this module type
	 */
	public static int getModuleTypeSize(int type)
	{
		return ModuleList.mList.get(type).size();
	}
	/**
	 * This method will add in the passed in module to the correct module list.
	 * @param m the module to be added onto the list.
	 */
	public static void addModule(Module m)
	{
		//check module "m" what type it is and insert it to the correct list
		int index = checkModuleType(m);
		//add module "m" to the correct list
		ModuleList.mList.get(index).add(m);
		
	}
	/**
	 * This method will remove the module from the list, if it's on the list.
	 * @param m the module to be removed from list.
	 * @return true if remove successful, else return false.
	 */
	public static boolean removeModule(Module m)
	{
		//check module "m" what type it is and insert it to the correct list
		int index = checkModuleType(m);
		//remove module "m" from the correct list
		return ModuleList.mList.get(index).remove(m);
	}
	/**
	 * This method will check the module type and correctly return the index
	 * to the module list containing that module type.
	 * @param m
	 * @return the index to the correct module list
	 */
	private static int checkModuleType(Module m)
	{
		//returns the correct index to the module list
		return m.getModuleType().ordinal();
	}
	/**
	 * 
	 * @return 0 for no possible configuration, 1 for min, and 2 for max possible configuration
	 */
	public static int checkConfiguration(){
		/**	usable[0] = 0;//plain
			usable[1] = 0;//control
			usable[2] = 0;//power
			usable[3] = 0;//food water storage
			usable[4] = 0;//dormitory
			usable[5] = 0;//canteen
			usable[6] = 0;//sanitation
			usable[7] = 0;//gym --dont care
			usable[8] = 0;//airlock
			usable[9] = 0;//medical */
			for(int i=0;i<Module.MAX_SIZE;i++)
			{
				usable[i]=0;
			}
			//now we'll check how many necessary modules for min configuration are usable
			
				for(int i=0;i<10;i++){
					for(int k=0;k<mList.get(i).size();k++){
						if(!mList.get(i).get(k).getStats().equals("beyond repair"))
							usable[i]++;
					}
				}
				if(usable[0]>=3 && usable[1]>=1 && usable[2]>=1 && usable[3]>=1 &&
						usable[4]>=1 && usable[5]>=1 &&usable[6]>=1 && usable[8]>=1) {
					return 1;
					//Window.alert("A minimum configuration is now possible!");
				}
			return 0;
			}
}
